package com.cskaoyan.util;

import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class SessionUtil {

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketAdminMapper marketAdminMapper;

    private static MarketUserMapper staticUserMapper;
    private static MarketAdminMapper staticAdminMapper;

    @PostConstruct
    public void init() {
        staticUserMapper = this.marketUserMapper;
        staticAdminMapper = this.marketAdminMapper;
    }

    public static MarketUser getUserInfo() {
        //        获取用户名

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
//        if (username == null || "".equals(username)) {
//            return null;
//        }
        if (!ParamUtils.isValid(username)) {
            return null;
        }
        MarketUserExample userExample = new MarketUserExample();
        userExample.createCriteria().andUsernameEqualTo(username);

        // TODO: 2022/10/14 是否需要静态注入
        /**
         * solution:
         *      private static UserMapper staticUserMapper;
         *      staticUserMapper = this.userMapper;
         */


        List<MarketUser> users = staticUserMapper.selectByExample(userExample);

        //拿到具体用户的登录信息
        MarketUser marketUser = users.get(0);
        return marketUser;
    }


    public static MarketAdmin getAdminInfo() {
        //        获取用户名

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
//        if (username == null || "".equals(username)) {
//            return null;
//        }
        if (!ParamUtils.isValid(username)) {
            return null;
        }
        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.createCriteria().andUsernameEqualTo(username);

        // TODO: 2022/10/14 是否需要静态注入
        /**
         * solution:
         *      private static UserMapper staticUserMapper;
         *      staticUserMapper = this.userMapper;
         */


        List<MarketAdmin> users = staticAdminMapper.selectByExample(adminExample);

        //拿到具体用户的登录信息
        MarketAdmin marketUser = users.get(0);
        return new MarketAdmin();
    }
}
